package io.renren.modules.customer.controller;

import java.util.*;
import java.util.stream.Collectors;

import io.renren.common.annotation.SysLog;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.customer.entity.CustomerEntity;
import io.renren.modules.customer.service.BatchApplicationService;
import io.renren.modules.customer.service.CustomerService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.customer.entity.MessageReplyEntity;
import io.renren.modules.customer.service.MessageReplyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Sean
 * @email seanla@qq.com
 * @date 2020-03-17 19:29:26
 */
@RestController
@RequestMapping("customer/messagereply")
public class MessageReplyController extends AbstractController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageReplyController.class);

    @Autowired
    private MessageReplyService messageReplyService;

    @Autowired
    private BatchApplicationService batchApplicationService;

    @Autowired
    private CustomerService customerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("customer:messagereply:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = messageReplyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("customer:messagereply:info")
    public R info(@PathVariable("id") Long id){
		MessageReplyEntity messageReply = messageReplyService.getById(id);

        return R.ok().put("messageReply", messageReply);
    }

    /**
     * 保存
     */
    @SysLog("新增短信回复")
    @RequestMapping("/save")
    @RequiresPermissions("customer:messagereply:save")
    public R save(@RequestBody MessageReplyEntity messageReply){
        ValidatorUtils.validateEntity(messageReply);
		messageReplyService.save(messageReply);
        messageReply.setCreateUserId(getUserId());
        messageReply.setCreateTime(new Date());
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改短信回复")
    @RequestMapping("/update")
    @RequiresPermissions("customer:messagereply:update")
    public R update(@RequestBody MessageReplyEntity messageReply){
        ValidatorUtils.validateEntity(messageReply);
		messageReplyService.updateById(messageReply);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除短信回复")
    @RequestMapping("/delete")
    @RequiresPermissions("customer:messagereply:delete")
    public R delete(@RequestBody Long[] ids){
		messageReplyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 提交申请进件
     */
    @SysLog("提交申请进件")
    @RequestMapping("/batch-apply")
    @RequiresPermissions("customer:messagereply:batchapply")
    public R submitToApplication(@RequestBody Long[] ids) {
        List<MessageReplyEntity> replyEntities = this.messageReplyService.getRepliesByIds(Arrays.asList(ids));
        if (replyEntities != null && replyEntities.size() > 0) {
            List<CustomerEntity> customerEntities = this.customerService.getCustomersByMobilePhones(replyEntities.stream().map(MessageReplyEntity::getMobilePhone).collect(Collectors.toList()));
            //获得批次号与手机的关系
            Map<String, String> batchNumbers = new HashMap<>();
            for (MessageReplyEntity replyEntity : replyEntities) {
                batchNumbers.put(replyEntity.getMobilePhone(), replyEntity.getBatchNumber());
            }
            if (customerEntities != null && customerEntities.size() > 0) {
                this.batchApplicationService.submitToApply(batchNumbers, customerEntities, getUserId());
            }
        }
        return R.ok();
    }
}
